<template>
  <el-dialog
      top="5vh"
      width="1000px"
      title="添加/编辑文章"
      :visible.sync="dialogValue"
      :close-on-click-modal="false"
      append-to-body
  >
    <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @change="handleFormChange"
        @save="onSave"
        @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import {http} from '@/api'

export default {
  name: 'ArticleAdd',
  data() {
    return {
      apiKey: 'article',
      dialogValue: false,
      ruleForm: {},
      defaultForm: {
        title: '',
        author: '',
        content: '',
        summary: '',
        cover: '',
        category_id: '',
        tags: '[]',
        view_count: 0,
        like_count: 0,
        sort: 0,
        status: 1,
        is_top: 0,
        is_recommend: 0,
        publish_time: null
      },
      rules: {
        title: [{required: true, message: '文章标题不能为空'}],
        author: [{required: true, message: '作者不能为空'}],
        content: [{required: true, message: '文章内容不能为空'}],
        category_id: [{required: true, message: '文章分类不能为空'}],
      },
      formList: [
        {
          label: '文章标题',
          prop: 'title',
          name: 'input',
          colSpan: 24,
          placeholder: '请输入文章标题'
        },
        {
          label: '作者',
          prop: 'author',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入作者'
        },
        {
          label: '文章分类',
          prop: 'category_id',
          name: 'select',
          colSpan: 12,
          placeholder: '请选择文章分类',
          options: []
        },
        {
          label: '封面图片',
          prop: 'cover',
          name: 'file',
          colSpan: 24
        },
        {
          label: '文章摘要',
          prop: 'summary',
          name: 'input',
          colSpan: 24,
          rows: 3,
          placeholder: '请输入文章摘要'
        },
        {
          label: '标签',
          prop: 'tags',
          name: 'input',
          colSpan: 24,
          placeholder: '请输入标签，JSON格式，如：["技术","前端","Vue"]'
        },
        {
          label: '文章内容',
          prop: 'content',
          name: 'rich',
          colSpan: 24
        },
        {
          label: '浏览次数',
          prop: 'view_count',
          name: 'number',
          colSpan: 8,
          min: 0
        },
        {
          label: '点赞次数',
          prop: 'like_count',
          name: 'number',
          colSpan: 8,
          min: 0
        },
        {
          label: '排序',
          prop: 'sort',
          name: 'number',
          colSpan: 8,
          min: 0
        },
        {
          label: '发布时间',
          prop: 'publish_time',
          name: 'datetime',
          colSpan: 12,
          dateType: 'datetime',
          valueFormat: 'YYYY-MM-DD HH:mm:ss'
        },
        {
          label: '发布状态',
          prop: 'status',
          name: 'switch',
          colSpan: 12,
          activeValue: 1,
          inactiveValue: 0,
          activeText: '发布',
          inactiveText: '草稿'
        },
        {
          label: '是否置顶',
          prop: 'is_top',
          name: 'switch',
          colSpan: 12,
          activeValue: 1,
          inactiveValue: 0,
          activeText: '置顶',
          inactiveText: '不置顶'
        },
        {
          label: '是否推荐',
          prop: 'is_recommend',
          name: 'switch',
          colSpan: 12,
          activeValue: 1,
          inactiveValue: 0,
          activeText: '推荐',
          inactiveText: '不推荐'
        },
      ]
    }
  },
  mounted() {
    this.loadCategories();
  },
  methods: {
    loadCategories() {
      // 加载文章分类
      http.articleCategory.list().then(res => {
        this.formList[2].options = res.data.map(item => ({
          value: item.id,
          label: item.name
        }));
      });
    },
    handleFormChange({key, val}) {
      // 处理表单变化
    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data,
            tags: res.data.tags || '[]'
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm,
          publish_time: new Date().toISOString().slice(0, 19).replace('T', ' ')
        }
      }
    },
    onSave() {
      // 验证标签JSON格式
      try {
        if (this.ruleForm.tags) {
          JSON.parse(this.ruleForm.tags);
        }
      } catch (e) {
        this.$message.error('标签JSON格式不正确');
        return;
      }
      
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, this.ruleForm).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style> 